<template>
	<view>
		<view class="edgeInsetTop"></view>

		<view class="delivery_type">
			<view class="btn disabled">物流快递</view>
			<view class="btn active">到店自提</view>
		</view>
		<view class="wanl-order">
			<view class="lists bg-white margin-bj radius-bock">
				<view class="shopname">
					<text class="wlIcon-dianpu2 text-orange margin-right-sm"></text>
					<text class="wanl-pip text-df">哈尔滨店铺</text>
				</view>
				<view class="cu-list menu-avatar">
					<view class="cu-item margin-bottom">
						<!-- <view class="cu-avatar radius-bock"
							:style="{ backgroundImage: 'url(' + $wanlshop.oss(goods.image, 77, 77) + ')' }"></view> -->
						
							<image class="cu-avatar radius-bock" :src="con.shangpintu" mode="aspectFill"></image>
						<view class="content">
							<view class="text-sm text-cut-2">{{con.shangpinming}}</view>
							<view class="wanl_sku text-sm">
								<text>
									{{con.shangpinmiaoshu}}
								</text>
							</view>
						</view>
						<view class="action">
							<view class="wanl-pip text-sm text-price waw">{{con.jiage}}</view>
							<!-- <view class="wanl-pip text-sm text-price">{{con.shangpinyuanjia}}</view> -->
							<view class="wanl-gray-light text-sm">x1</view>
						</view>
					</view>
					<form>
						<view class="cu-form-group align-start">
							<view class="wanl-gray-light title">备注</view>
							<textarea maxlength="-1" placeholder="订单备注可选"></textarea>
						</view>
					</form>
					<view class="text-right margin-bj text-sm">
						<text class="wanl-gray">共1件，</text>
						小计：
						<text class="text-price text-orange">{{con.jiage}}</text>
					</view>
				</view>
			</view>
			<view class="safeAreaBottom"> </view>
			<view class="wanlian cu-bar tabbar solid-top foot text-df">
				<view>
					<text class="wanl-gray">共1件，</text>
					合计：
					<text class="text-price text-orange">{{con.jiage}}</text>
				</view>
				<button v-if="loading" class="cu-btn round lg bg-gradual-orange margin-lr-bj">
					<text class="wlIcon-jiazai wlIconfont-spin margin-right-xs"></text> 提交中...
				</button>
				<button v-else @tap="addOrder" class="cu-btn round lg bg-gradual-orange margin-lr-bj"> 提交订单 </button>
			</view>
		</view>
	</view>
</template>

<script>
	import wanlshop_config from '@/common/config/config.js';
	let cdnurl = wanlshop_config.cdnurl, appurl = wanlshop_config.appurl, header
	export default {
		data() {
			return {
				con: {}
			};
		},
		onLoad(option) {
			this.con = JSON.parse(option.canshu)
			console.log(this.con, '999999')
		},
		methods: {
			// 提交订单
			addOrder() {
				uni.navigateTo({
					url: `/pages/user/money/pay`
				})
				// if (this.orderData.statis.allnum === 0) {
				// 	this.$wanlshop.msg('订单异常')
				// 	return;
				// }
				// let address_id = this.addressData.id;
				// if (address_id === 0) {
				// 	this.$wanlshop.msg('请填写地址')
				// 	return;
				// }
				// let data = {
				// 		lists: [],
				// 		address_id: address_id
				// 	},
				// 	cart = [];
				// this.orderData.lists.forEach((item, index) => {
				// 	data.lists.push({
				// 		shop_id: item.shop_id,
				// 		remarks: item.remarks,
				// 		products: [],
				// 		//店铺优惠券和其他 在下追加
				// 		coupon_id: this.couponData[index] ? this.couponData[index].id : 0
				// 	});
				// 	item.products.forEach(goods => {
				// 		// 购物车数据
				// 		cart.push({
				// 			goods_id: goods.id,
				// 			sku_id: goods.sku.id
				// 		});
				// 		// 商品数据
				// 		data.lists[index].products.push({
				// 			goods_id: goods.id,
				// 			number: goods.number,
				// 			sku_id: goods.sku.id,
				// 			freight_id: goods.freight_id
				// 		});
				// 	});
				// });
				// this.loading = true;
				// // 提交订单数据data
				// await uni.request({
				// 	url: '/wanlshop/order/addOrder',
				// 	method: 'POST',
				// 	data: {
				// 		order: data,
				// 		sup_id: this.sup_id,
				// 		deliveryType: this.selectedType,
				// 		token: this.token
				// 	},
				// 	success: res => {
				// 		this.$store.commit('statistics/order', {
				// 			pay: this.$store.state.statistics.order.pay + res.data.list.length,
				// 			whole: this.$store.state.statistics.order.whole + res.data.list.length
				// 		});
				// 		// 如果使用优惠券全局减去
				// 		let coupon = Object.keys(this.couponData).length;
				// 		if (coupon != 0) {
				// 			this.$store.commit('statistics/dynamic', {
				// 				coupon: this.$store.state.statistics.dynamic.coupon - coupon
				// 			});
				// 		}
				// 		if (this.cartType == 'cart') {
				// 			this.$store.dispatch('cart/del');
				// 		}
				// 		this.$wanlshop.to(
				// 			`/pages/user/money/pay?token=${res.data.token}&data=${JSON.stringify(res.data.list)}&sup_id=${this.sup_id}`
				// 		);
				// 	}
				// });
			},
			//添加或修改成功之后回调
			async refreshList(data, type) {
				await uni.request({
					url: '/wanlshop/address/address',
					method: 'POST',
					data: {
						data: data,
						type: 'add'
					},
					success: res => {
						this.addressData = res.data;
						// 1.1.6升级
						if (this.reload) {
							this.loadData();
						}
					}
				});
			},
			// 查询优惠券，减缓服务器压力，单独查询（仅查询首次）
			async queryCoupon(index) {
				let data = this.orderData.lists[index];
				if (data.coupon.length == 0) {
					let goods_id = [];
					let shop_category_id = [];
					for (var i = 0; i < data.products.length; i++) {
						goods_id.push(data.products[i]['id']);
						shop_category_id.push(data.products[i]['shop_category_id']);
					};
					await uni.request({
						url: '/wanlshop/coupon/query',
						method: 'POST',
						data: {
							shop_id: data.shop_id,
							goods_id: this.unique(goods_id),
							shop_category_id: this.unique(shop_category_id),
							price: data.order_price
						},
						success: res => {
							data.coupon = res.data;
						}
					});
				}
				this.couponIndex = index;
				this.modalName = 'coupon';
			},
			// 领取优惠券
			async onReceive(index) {
				let coupon = this.orderData.lists[this.couponIndex].coupon[index];
				await uni.request({
					url: '/wanlshop/coupon/receive',
					method: 'POST',
					data: {
						id: coupon.id
					},
					success: res => {
						coupon.id = res.data.id;
						coupon.state = true;
						this.$wanlshop.msg(res.data.msg);
						this.$store.commit('statistics/dynamic', {
							coupon: this.$store.state.statistics.dynamic.coupon + 1
						});
					}
				});
			},
			// 选中优惠券, 将优惠券放进数组中
			onCoupons(index) {
				let order = this.orderData.lists[this.couponIndex];
				let data = this.orderData.lists[this.couponIndex].coupon;
				data[index].choice = !data[index].choice;
				if (data[index].choice) {
					// 遍历取消掉其他选择的状态，for性能高于其他历遍
					for (var i = 0; i < data.length; i++) {
						if (i != index) {
							data[i].choice = false;
						}
					};
					// 更新选择
					this.couponData[this.couponIndex] = data[index];
					// @ 精度计算 this.$wanlshop.bcadd('0.132123',0.132123)
					// 满减计算
					if (data[index].type == 'reduction' || (data[index].type == 'vip' && data[index].usertype ==
							'reduction')) {
						order.freight.price = order.freight_price_bak;
						order.sub_price = this.$wanlshop.bcsub(this.$wanlshop.bcadd(order.order_price, order.freight
							.price), data[index].price);
						if (order.sub_price < 0) {
							order.sub_price = 0.01;
						}
					}
					// 折扣计算  前端出问题
					if (data[index].type == 'discount' || (data[index].type == 'vip' && data[index].usertype ==
							'discount')) {
						let discount = data[index].discount > 10 ? this.$wanlshop.bcdiv(data[index].discount, 100) : this
							.$wanlshop.bcdiv(data[index].discount, 10);
						order.freight.price = order.freight_price_bak;
						order.sub_price = this.$wanlshop.bcadd(this.$wanlshop.bcmul(order.order_price, discount), order
							.freight.price);
					}
					// 包邮计算
					if (data[index].type == 'shipping') {
						order.freight.price = 0;
						order.sub_price = order.order_price;
					}
				} else {
					// 恢复原价 1.0.2升级
					this.couponData = {};
					if (data[index].type == 'shipping') {
						order.freight.price = order.freight_price_bak;
					}
					order.sub_price = this.$wanlshop.bcadd(order.order_price, order.freight.price);
				}
				// 精度计算合计
				this.orderData.statis.allsub = 0;
				for (var i = 0; i < this.orderData.lists.length; i++) {
					this.orderData.statis.allsub = this.$wanlshop.bcadd(this.orderData.statis.allsub, this.orderData.lists[
						i].sub_price);
				};
				this.modalName = null;
			},
			// 修改数量
			changeNum(num) {
				num = num == 0 ? 1 : num;
				this.orderData.lists[0].number = num;
				this.orderData.lists[0].products[0].number = num;
				this.orderData.statis.allnum = num;
				// 计算价格
				this.orderData.lists[0].sub_price = this.$wanlshop.bcmul(this.orderData.lists[0].products[0].sku.price,
					num);
				this.orderData.statis.allsub = this.$wanlshop.bcmul(this.orderData.lists[0].products[0].sku.price, num);
			},
			addAddress(type) {
				this.$wanlshop.to(`/pages/user/address/addressManage?type=${type}`);
			},
			userAddress() {
				this.$wanlshop.to('/pages/user/address/address?source=1');
			},
			// 弹出层，暂时不需要但后续版本可能需要
			showModal(name) {
				this.modalName = name;
			},
			// 关闭弹出层
			hideModal() {
				this.modalName = null;
			},
			// 数组去重并字符串
			unique(arr) {
				return Array.from(new Set(arr)).join(',');
			},
			//禁止父元素滑动
			moveHandle() {}
		}
	};
</script>

<style scoped>
	.select-address {
		width: 54rpx;
		height: 54rpx;
	}

	.delivery_type {
		display: flex;
		justify-content: center;
		gap: 10px;
	}

	.btn {
		width: 334.33rpx;
		height: 82rpx;
		border-radius: 20px;
		border: 1px solid #84BD00;
		color: #84BD00;
		background-color: #fff;
		text-align: center;
		font-size: 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.btn.active {
		background-color: #84BD00;
		color: #fff;
	}

	/* 禁用状态 */
	.btn.disabled {
		color: #999;
		border-color: #ccc;
		background-color: #f2f2f2;
		cursor: not-allowed;
	}
</style>